//
//  KKSwitchBarView.m
//  掌中陕西
//
//  Created by Kratos on 14-8-7.
//  Copyright (c) 2014年 RM. All rights reserved.
//

#import "KKSwitchBarView.h"

#define kDefaultButtonTag 1000

@implementation FontSizeButtonObject
@end

@interface KKSwitchBarView ()
@property (nonatomic , strong) NSMutableArray *setupBtnInfoArray;
@property (nonatomic , strong) NSMutableArray *setupBtnArray;
@end

@implementation KKSwitchBarView

- (id)initWithFontSizeBtnInfo:(NSArray *) fontSizeInfo selectedIndex:(NSInteger) selected frame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setBackgroundColor:[UIColor clearColor]];
        [self setFontSizeBtns:fontSizeInfo selectedIndex:selected];
        
    }
    return self;
}

/**
 * 设置添加界面上的按钮
 * @param fontSizeInfo 字体信息
 * @return void
 * @author Kratos
 */
- (void)setFontSizeBtns:(NSArray *)fontSizeInfo selectedIndex:(NSInteger) selected
{
    float btnHeight = CGRectGetHeight(self.bounds);
    float btnWidth = CGRectGetWidth(self.bounds)/[fontSizeInfo count];
    
    self.setupBtnInfoArray = [NSMutableArray arrayWithArray:fontSizeInfo];
    self.setupBtnArray  = [[NSMutableArray alloc]initWithCapacity:0];;
    for (int index = 0; index < [fontSizeInfo count]; index++) {
        UIButton *sizebtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [sizebtn setBackgroundColor:[UIColor clearColor]];
        sizebtn.tag = kDefaultButtonTag + index;
        [sizebtn setFrame:CGRectMake(btnWidth * index, 0.f, btnWidth, btnHeight)];
        
        FontSizeButtonObject *btnInfo = (FontSizeButtonObject *)[fontSizeInfo objectAtIndex:index];
        NSString *title = btnInfo.buttonTitle;
        UIColor *titleDefaultColor = btnInfo.buttonTitleDefaultColor;
        UIColor *titleSelectedColor = btnInfo.buttonTitleSelectedColor;
        NSString *defaultImage = btnInfo.buttonDefaultImage;
        NSString *selectedImage = btnInfo.buttonSelectedImage;
        NSString *backgroudDefaultImage = btnInfo.buttonBackgroudDefaultImage;
        NSString *backgroudSelectedImage = btnInfo.buttonBackgroudSelectedImage;
        NSInteger fontsize = btnInfo.fontSize;
        
        if (title)
            [sizebtn setTitle:title forState:UIControlStateNormal];
        
        if (titleDefaultColor)
            [sizebtn setTitleColor:titleDefaultColor forState:UIControlStateNormal];
        
        if (titleSelectedColor)
            [sizebtn setTitleColor:titleSelectedColor forState:UIControlStateSelected];
        
        if (defaultImage)
            [sizebtn setImage:[UIImage imageNamed:defaultImage] forState:UIControlStateNormal];
        
        if (selectedImage)
            [sizebtn setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
        
        if (backgroudDefaultImage)
            [sizebtn setBackgroundImage:[UIImage imageNamed:backgroudDefaultImage] forState:UIControlStateNormal];
        
        if (backgroudSelectedImage)
            [sizebtn setBackgroundImage:[UIImage imageNamed:backgroudSelectedImage] forState:UIControlStateSelected];
        
        if (fontsize > 0) {
            [sizebtn.titleLabel setFont:[UIFont systemFontOfSize:fontsize]];
        }
        
        
        [sizebtn addTarget:self action:@selector(reloadFontBtns:) forControlEvents:UIControlEventTouchUpInside];
        
        if (selected == index) {
            [sizebtn setSelected:YES];
        }
        
        [self.setupBtnArray addObject:sizebtn];
        
        [self addSubview:sizebtn];
    }
}


- (void)reloadFontBtns:(id)sender

{
    UIButton *selectedBtn = (UIButton *)sender;
    
    if (![selectedBtn isSelected]) {
        for (UIButton *btn in self.setupBtnArray) {
            if ([btn isEqual:selectedBtn]) {
                [btn setSelected:YES];
            }
            else
            {
                [btn setSelected:NO];
            }
        }
    }
    
    if (_selectedBlock) {
        _selectedBlock(selectedBtn.tag - kDefaultButtonTag);
    }
    
    if ([_delegate respondsToSelector:@selector(switchBarSelectedIndex:switchBar:)]) {
        [_delegate switchBarSelectedIndex:selectedBtn.tag - kDefaultButtonTag switchBar:self];
    }
}

- (void)setDefaultSelectedIndex:(NSInteger)index
{
    if (index < [self.setupBtnArray count]) {
        for (UIButton *btn in self.setupBtnArray) {
            [btn setSelected:NO];
        }
        
        UIButton *selectedBtn = (UIButton *)self.setupBtnArray[index];
        [selectedBtn setSelected:YES];
    }
    

}

@end
